package com.ctgu.admin.system.controller;

import com.ctgu.admin.system.domain.WebTips;
import com.ctgu.admin.system.service.WebTipsService;
import com.ctgu.admin.system.validator.WebTipsValid;
import com.ctgu.common.enums.StatusEnum;
import com.ctgu.common.utils.EntityBeanUtil;
import com.ctgu.common.utils.ResultVoUtil;
import com.ctgu.common.utils.StatusUtil;
import com.ctgu.common.vo.ResultVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该类负责网站气泡提示
 */
@Controller
@RequestMapping("/system/webTips")
public class WebTipsController {

    @Autowired
    private WebTipsService webTipsService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("system:webTips:index")
    public String index(Model model, WebTips webTips) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("title", match -> match.contains())
                .withMatcher("content", match -> match.contains());

        // 获取数据列表
        Example<WebTips> example = Example.of(webTips, matcher);
        Page<WebTips> list = webTipsService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/system/webTips/index";
    }

    //4.根据ID查询一条网页消息
    @RequestMapping("/get/{id}")
    @ResponseBody
//    @ApiOperation(value = "根据id一条网页通知", notes = "查询数据库中某个的网页通知")
//    @ApiImplicitParam(name = "id", value = "商品ID",  paramType = "path", required = true, dataType =  "Integer")
    public Map<String,Object> getCategory(@PathVariable Long id) { //@PathVariable 获取/get/{id}中id
        //System.out.println("有人打算获取网页提示信息，ID:"+id);
        WebTips webTips = webTipsService.getById(id);
        Map<String,Object> result = new HashMap<String,Object>();
        if (webTips == null) {
            try {
                throw new Exception("网页通知ID：" + id +  "，未找到");
            } catch (Exception e) {
                e.printStackTrace();
            }
            result.put("respCode", "02");
            result.put("respMsg", "数据库无该网页信息");
            result.put("webTips", webTips);
            return result;
        }else{
            result.put("respCode", "01");
            result.put("respMsg", "查询成功！");
            result.put("webTips", webTips);
            return result;
        }
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("system:webTips:add")
    public String toAdd() {
        return "/system/webTips/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:webTips:edit")
    public String toEdit(@PathVariable("id") WebTips webTips, Model model) {
        model.addAttribute("webTips", webTips);
        return "/system/webTips/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"system:webTips:add", "system:webTips:edit"})
    @ResponseBody
    public ResultVo save(@Validated WebTipsValid valid, WebTips webTips) {
        // 复制保留无需修改的数据
        if (webTips.getId() != null) {
            WebTips beWebTips = webTipsService.getById(webTips.getId());
            EntityBeanUtil.copyProperties(beWebTips, webTips);
        }

        // 保存数据
        webTipsService.save(webTips);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:webTips:detail")
    public String toDetail(@PathVariable("id") WebTips webTips, Model model) {
        model.addAttribute("webTips",webTips);
        return "/system/webTips/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("system:webTips:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (webTipsService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}